$(function () {
    var formSelects = layui.formSelects;
    var layer;
    layui.use(['layer', 'form'], function(){
        layer = layui.layer
        var form = layui.form;
        form.on('select(single)', function (data) {
            var formSelects = layui.formSelects;
            var elec_collectors=formSelects.value('select1', 'val').toString();
            if (elec_collectors==null||elec_collectors==""){
                layer.msg('请先选择表计', {icon: 7,time:2000});
            }
        });
    });
    function renderForm(){
        layui.use('form', function(){
            var form = layui.form;
            form.render();
        });
    }
    $.ajax({
        type: 'POST',
        url: '/openAccount/getCollectPoint',
        async: false,
        data: {},
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var collectCount = result.data.meterList.length;
                var regionCount = result.data.regionList.length;
                for (i = 0;i<collectCount;i++) {
                    $(".collect_value").append("<option value=" +  result.data.meterList[i].id + ">" + result.data.meterList[i].id+ "</option>");
                }
                for (i = 0;i<regionCount;i++) {
                    $("#s_area").append("<option value=" +  result.data.regionList[i].id + ">" + result.data.regionList[i].name+ "</option>");
                }
                formSelects.render()
            }else{
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
    //是否需要公摊进行隐藏
    var code1 = $("#departSelect").val();
    if(code1==0){
        $('.mer_area').css('display','none')
    }else{
        $('.share').css('display','block')
    }
});
$('#next').click(function () {
    var name = $('input[name="name"]').val();
    var code1 = $("#departSelect").val()
    var pwd = $('input[name="pwd"]').val();
    var confirm = $('input[name="confirm"]').val();
    var p1=$('input[name="p1"]').val();
    var phone1=$('input[name="phone1"]').val();
    var square=$('input[name="square"]').val();
    var allsquare=$('input[name="allsquare"]').val();
    if(pwd == '' || name == ''){
        layer.msg('用户名或密码不能为空！请重新输入！', {icon: 5});
    }else if(pwd !=confirm){
        layer.msg('两次输入密码不一致！请重新输入！', {icon: 5});
    }else if(p1==''){
        layer.msg('第一联系人不能为空', {icon: 5});
    }else if(phone1==''){
        layer.msg('第一联系人手机号不能为空', {icon: 5});
    }else if(code1==1&&square==''){
         layer.msg('商铺面积不能为空', {icon: 5});
     }else  if(code1==1&&allsquare=='') {
            layer.msg('总面积不能为空', {icon: 5});
    }else{
        $('.merchantname').html('账户名称：'+$('input[name="name"]').val());
        $('.body1').css('display','none');
        $('.body2').css('display','block');
    }
});
$('.back').click(function () {
    $('.body1').css('display','block');
    $('.body2').css('display','none');
});
//手机号校验
function checkMobile(obj) {
    var  re = /^1\d{10}$/;
    if (!re.test($(obj).val())) {
        layer.msg('请输入合法手机号！', {icon: 5});
    }
}
    $("#departSelect").change(function() {
        var code=$("#departSelect").val();
        if(code==0){
            $('.mer_area').css('display','none')
        }else{
            $('.mer_area').css('display','block')
        }
    });
$('#save').on('click',function () {
    var formSelects = layui.formSelects;
    var name = $('input[name="name"]').val();
    var code1 = $("#departSelect").val()
    var pwd = $('input[name="pwd"]').val();
    var p1=$('input[name="p1"]').val();
    var phone1=$('input[name="phone1"]').val();
    var addr1=$('input[name="addr1"]').val();
    var mail1=$('input[name="mail1"]').val();
    var p2=$('input[name="p2"]').val();
    var phone2=$('input[name="phone2"]').val();
    var addr2=$('input[name="addr2"]').val();
    var mail2=$('input[name="mail2"]').val();
    var square=$('input[name="square"]').val();
    var allsquare=$('input[name="allsquare"]').val();
    var area=$('select[name="area_id"]').val();
    <!--电子账户-->
    var elec_name=$('input[name="elec_name"]').val();
    var elec_power_type=$('select[name="elec_power_type"]').val();
    var elec_collectors=formSelects.value('select1', 'val').toString();
    var elec_acquire_type=$('select[name="elec_acquire_type"]').val();
    var elec_init_reading=$('input[name="elec_init_reading"]').val();
    var elec_charge=$('select[name="elec_charge"]').val();
    <!--水子账户-->
    var water_name=$('input[name="water_name"]').val();
    var water_power_type=$('select[name="water_power_type"]').val();
    var water_collectors=formSelects.value('select2', 'val').toString();
    var water_acquire_type=$('select[name="water_acquire_type"]').val();
    var water_init_reading=$('input[name="water_init_reading"]').val();
    var water_charge=$('select[name="water_charge"]').val();
    <!--气子账户-->
    var gas_name=$('input[name="gas_name"]').val();
    var gas_power_type=$('select[name="gas_power_type"]').val();
    var gas_collectors=formSelects.value('select3', 'val').toString();
    var gas_acquire_type=$('select[name="gas_acquire_type"]').val();
    var gas_init_reading=$('input[name="gas_init_reading"]').val();
    var gas_charge=$('select[name="gas_charge"]').val();
    <!--公摊电费-->
    var shareelec_name=$('input[name="shareelec_name"]').val();
    var shareelec_power_type=$('select[name="shareelec_power_type"]').val();
    var shareelec_collectors=formSelects.value('select4', 'val').toString();
    var shareelec_acquire_type=$('select[name="shareelec_acquire_type"]').val();
    var shareelec_init_reading=$('input[name="shareelec_init_reading"]').val();
    var shareelec_charge=$('select[name="shareelec_charge"]').val();
    <!--公摊水费-->
    var sharewater_name=$('input[name="sharewater_name"]').val();
    var sharewater_power_type=$('select[name="sharewater_power_type"]').val();
    var sharewater_collectors=formSelects.value('select5', 'val').toString();
    var sharewater_acquire_type=$('select[name="sharewater_acquire_type"]').val();
    var sharewater_init_reading=$('input[name="sharewater_init_reading"]').val();
    var sharewater_charge=$('select[name="sharewater_charge"]').val();
    <!--公摊燃气-->
    var sharegas_name=$('input[name="sharegas_name"]').val();
    var sharegas_power_type=$('select[name="sharegas_power_type"]').val();
    var sharegas_collectors=formSelects.value('select6', 'val').toString();
    var sharegas_acquire_type=$('select[name="sharegas_acquire_type"]').val();
    var sharegas_init_reading=$('input[name="sharegas_init_reading"]').val();
    var sharegas_charge=$('select[name="sharegas_charge"]').val();
    $.ajax({
        type: 'POST',
        url: '/openAccount/saveAccount',
        async: false,
        data: {"name":name,"code1":code1,"pwd":pwd,"p1":p1,"phone1":phone1,"addr1":addr1,"mail1":mail1,
            "p2":p2,"phone2":phone2,"addr2":addr2,"mail2":mail2,"square":square,"allsquare":allsquare,"area":area,
            "elecpowertype":elec_power_type,"eleccollectors":elec_collectors,"elecacquiretype":elec_acquire_type,"elecinitreading":elec_init_reading,"eleccharge":elec_charge,
            "waterpowertype":water_power_type,"watercollectors":water_collectors,"wateracquiretype":water_acquire_type,"waterinitreading":water_init_reading,"watercharge":water_charge,
            "gaspowertype":gas_power_type,"gascollectors":gas_collectors,"gasacquiretype":gas_acquire_type,"gasinitreading":gas_init_reading,"gascharge":gas_charge,
            "shareelecpowertype":shareelec_power_type,"shareeleccollectors":shareelec_collectors,"shareelecacquiretype":shareelec_acquire_type,"shareelecinitreading":shareelec_init_reading,"shareeleccharge":shareelec_charge,
            "sharewaterpowertype":sharewater_power_type,"sharewatercollectors":sharewater_collectors,"sharewateracquiretype":sharewater_acquire_type,"sharewaterinitreading":sharewater_init_reading,"sharewatercharge":sharewater_charge,
            "sharegaspowertype":sharegas_power_type,"sharegascollectors":sharegas_collectors,"sharegasacquiretype":sharegas_acquire_type,"sharegasinitreading":sharegas_init_reading,"sharegascharge":sharegas_charge,
            "elecname":elec_name,"watername":water_name,"gasname":gasname,"shareelecname":shareelec_name,"sharewatername":sharewater_name,"sharegasname":sharegas_name
        },
        dataType: 'json',
        success: function (result) {
            if (result.code==666){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('开户成功',{icon:1}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="index.jsp";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('开户失败',{icon:2});

                });
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (XMLHttpRequest.status==555){
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('未登录,请登录后操作',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                        top.location.href="/login";
                    });
                });
            }else{
                var layer;
                layui.use(['layer', 'form'], function(){
                    layer = layui.layer
                    layer.alert('页面加载失败',{icon:7}, function(index){
                        //do something
                        layer.close(index);
                    });
                });
            }
        },
    });
});